//
//  ViewController.m
//  课后作业—画板
//
//  Created by Jorn.Wu on 15/8/5.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "ViewController.h"
#import "JWDrawView.h"

#define JWScreenWidth [UIScreen mainScreen].bounds.size.width
#define JWScreenHeight [UIScreen mainScreen].bounds.size.height

#define JWControlBtnNum 7.f
#define JWControlBtnWidth JWScreenWidth / JWControlBtnNum
#define JWControlBtnHeight 45

#define JWControlViewHeight 114

#define JWSelectedColorBtnNum 9
#define JWControlBtnSpacing 5
#define JWSelectedColorBtnWidth (JWScreenWidth - JWControlBtnSpacing * (JWSelectedColorBtnNum + 1)) / JWSelectedColorBtnNum
#define JWSelectedColorBtnHeight (114 - 20 - 50)

#define JWColorWithRGB(r , g, b, a) [UIColor colorWithRed:(r) / 255.f green:(g) / 255.f blue:(b) / 255.f alpha:(a)]


@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UIView *controlView;
@property (nonatomic, strong) UIView *chooseView;
@property (nonatomic, strong) UISlider *lineWidthSlider;
@property (nonatomic, strong) UILabel *lineWidthValueLabel;

@property (nonatomic, strong) UIColor *currentColor;
@property (nonatomic, assign) CGFloat currentLineWidth;

@property (nonatomic, strong) UISlider *eraserSlider;
@property (nonatomic, strong) UILabel *eraserValueLabel;

@property (nonatomic, strong) JWDrawView *drawView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _currentColor = [UIColor blackColor];
    _currentLineWidth = 2.f;
    
    [self addControlView];
    [self addDrawView];
}

//---------------------创建顶部控制视图-----------------------------

- (void)addControlView{
    _controlView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth, 114)];
    _controlView.backgroundColor = [UIColor orangeColor];
    
    _chooseView = [[UIView alloc] initWithFrame:CGRectMake(0, 20 + JWControlBtnHeight, JWScreenWidth, JWControlViewHeight - 20 - JWControlBtnHeight)];
    [_controlView addSubview:_chooseView];
    
    NSArray *titleAr = @[@"照片",@"颜色",@"线宽",@"橡皮",@"撤销",@"清屏",@"保存"];
    
    for (int i = 0; i < JWControlBtnNum; i++) {
        
        UIButton *controlBtn = [self createControlBtnWith:CGRectMake(JWControlBtnWidth * i, 20, JWControlBtnWidth, JWControlBtnHeight) title:titleAr[i]];
        controlBtn.tag = 100 + i;
        [controlBtn addTarget:self action:@selector(controlBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [_controlView addSubview:controlBtn];
    }
    
    [self.view addSubview:_controlView];
}

//  创建btn

- (UIButton *)createControlBtnWith:(CGRect)frame title:(NSString *)title{
    
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:20];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn showsTouchWhenHighlighted];
    return btn;
}

//---------------------创建画板-----------------------------

- (void)addDrawView{
    
    _drawView = [[JWDrawView alloc] initWithFrame:CGRectMake(0, JWControlViewHeight, JWScreenWidth, JWScreenHeight - JWControlViewHeight)];
    
    [self.view addSubview:_drawView];
}




//--------------------------------------------------------

#pragma mark - btn响应方法

- (void)controlBtnAction:(UIButton *)btn{
    
    for (UIView *obj in _chooseView.subviews) {     //撤销_chooseView的子视图，从新加载
        [obj removeFromSuperview];
    }
    
    switch (btn.tag - 100) {      //切换到响应的按钮响应方法
        case 0:
            [self performSelector:@selector(imageBtnAction)];
            break;
        case 1:
            [self performSelector:@selector(colorBtnAction)];
            break;
        case 2:
            [self performSelector:@selector(lineWidthBtnAction)];
            break;
        case 3:
            [self performSelector:@selector(eraserBtnAction)];
            break;
        case 4:
            [self performSelector:@selector(undoBtnAction)];
            break;
        case 5:
            [self performSelector:@selector(clearBtnAction)];
            break;
        case 6:
            [self performSelector:@selector(saveBtnAction)];
            break;
        default:
            break;
    }
}

//---------------------颜色选择按钮-----------------------------

- (void)imageBtnAction{
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    
    [self presentViewController:picker animated:YES completion:nil];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    NSString *type = info[UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        
        UIImage *image = info[UIImagePickerControllerOriginalImage];
        [_drawView image:image];
        
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

//---------------------颜色选择按钮-----------------------------

- (void)colorBtnAction{
    
    NSArray *colorAr  = @[[UIColor blackColor],
                          [UIColor redColor],
                          [UIColor greenColor],
                          [UIColor yellowColor],
                          [UIColor blueColor],
                          [UIColor purpleColor],
                          [UIColor brownColor],
                          [UIColor cyanColor],
                          [UIColor whiteColor]];
    
    for (int i = 0; i < JWSelectedColorBtnNum; i++) {
        
        UIButton *seletedColorBtn = [[UIButton alloc] initWithFrame:CGRectMake((JWSelectedColorBtnWidth + JWControlBtnSpacing) * i + JWControlBtnSpacing, 0, JWSelectedColorBtnWidth, JWSelectedColorBtnHeight)];
        
        [seletedColorBtn addTarget:self action:@selector(selectedColorAction:) forControlEvents:UIControlEventTouchUpInside];
        seletedColorBtn.backgroundColor = colorAr[i];
        
        [_chooseView addSubview:seletedColorBtn];
    }
    
    
    [_drawView lineWidth:_currentLineWidth];        //恢复线宽
    [_drawView color:_currentColor];        //恢复颜色
}

- (void)selectedColorAction:(UIButton *)btn{
    _currentColor = btn.backgroundColor;
    [_drawView color:_currentColor];      //调用内部封装的方法
}

//---------------------线宽选择按钮-----------------------------

- (void)lineWidthBtnAction{
    
    _lineWidthSlider = [[UISlider alloc] initWithFrame:CGRectMake(10, 0, JWScreenWidth - 10 * 2 - 50, JWSelectedColorBtnHeight)];
    _lineWidthSlider.minimumValue = 1;
    _lineWidthSlider.maximumValue = 40;
    _lineWidthSlider.value = _currentLineWidth;     // 从上次保存的线宽开始
    [_lineWidthSlider addTarget:self action:@selector(lineWidthSliderAction:) forControlEvents:UIControlEventValueChanged];
    
    _lineWidthValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(JWScreenWidth - 10 - 50, 0, 50, JWSelectedColorBtnHeight)];
    _lineWidthValueLabel.textAlignment = NSTextAlignmentCenter;
    _lineWidthValueLabel.text = [NSString stringWithFormat:@"%.f点",_lineWidthSlider.value];
    
    [_chooseView addSubview:_lineWidthValueLabel];
    [_chooseView addSubview:_lineWidthSlider];
    
    
    [_drawView lineWidth:_currentLineWidth];        //恢复线宽
    [_drawView color:_currentColor];        //恢复颜色
    
}

- (void)lineWidthSliderAction:(UISlider *)slider{
    
    _lineWidthValueLabel.text = [NSString stringWithFormat:@"%.f点",slider.value];
    _currentLineWidth = slider.value;       //更新保存
    [_drawView lineWidth:_currentLineWidth];      //调用内部封装的方法
    
}

//---------------------颜色选择按钮-----------------------------

- (void)eraserBtnAction{
    
//      橡皮也要设置点数
    
    _eraserSlider = [[UISlider alloc] initWithFrame:CGRectMake(10, 0, JWScreenWidth - 10 * 2 - 50, JWSelectedColorBtnHeight)];
    _eraserSlider.value = 10;
    _eraserSlider.minimumValue = 10;
    _eraserSlider.maximumValue = 40;
    [_eraserSlider addTarget:self action:@selector(eraserSliderAction:) forControlEvents:UIControlEventValueChanged];
    
    _eraserValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(JWScreenWidth - 10 - 50, 0, 50, JWSelectedColorBtnHeight)];
    _eraserValueLabel.textAlignment = NSTextAlignmentCenter;
    _eraserValueLabel.text = [NSString stringWithFormat:@"%.f点",_eraserSlider.value];
    
    [_chooseView addSubview:_eraserValueLabel];
    [_chooseView addSubview:_eraserSlider];
    
    [_drawView lineWidth:10];       //默认是10点
    [_drawView eraser];      //调用内部封装的方法

}

- (void)eraserSliderAction:(UISlider *)slider{
    
    _eraserValueLabel.text = [NSString stringWithFormat:@"%.f点",slider.value];
    [_drawView lineWidth:slider.value];      //调用内部封装的方法
    
}

//---------------------颜色选择按钮-----------------------------

- (void)undoBtnAction{

    [_drawView undo];      //调用内部封装的方法
    
}

//---------------------颜色选择按钮-----------------------------

- (void)clearBtnAction{

    [_drawView clear];      //调用内部封装的方法
    
}

//---------------------颜色选择按钮-----------------------------

- (void)saveBtnAction{

    [_drawView save];      //调用内部封装的方法
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
